package authoritah.ui.spend;

import java.awt.BorderLayout;
import java.sql.Timestamp;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SpringLayout;


import authoritah.db.Event;
import authoritah.db.Item;
import authoritah.db.Member;
import authoritah.utils.SpringUtilities;

import com.toedter.calendar.JDateChooser;

public class SpendView extends JPanel {
	private SpendModel model;

	private JPanel spendDetails;
	private JDateChooser date;
	private JComboBox item;
	private JComboBox event;
	
	private JComboBox member;
	private JTextField pointsSpent;
	private JButton save;
	
	private JTable spends;
	private JScrollPane spendsScroll;
		
	public SpendView(SpendModel model) {
		super();
		this.model = model;
		
		initComponents();
		layoutComponents();
	}
	
	private void initComponents() {		
		event = new JComboBox(Event.getAllEvents().toArray());
		model.setEventModel(event.getModel());
		item = new JComboBox(Item.getAllItems().toArray());
		model.setItemModel(item.getModel());
		member = new JComboBox(Member.getAllMembers().toArray());
		model.setMemberModel(member.getModel());
		
		pointsSpent = new JTextField("", 2);
		model.setPointsDocument(pointsSpent.getDocument());
		date = new JDateChooser();
		save = new JButton("Save");
		
		spendDetails = new JPanel(new SpringLayout());
		
		spends = new JTable(model.getTableModel());
		spendsScroll = new JScrollPane(spends);
	}
	
	private void layoutComponents() {		
		spendDetails.add(new JLabel("Date"));
		spendDetails.add(date);
		spendDetails.add(new JLabel("Item"));
		spendDetails.add(item);
		spendDetails.add(new JLabel("Event"));
		spendDetails.add(event);
		spendDetails.add(new JLabel("Member"));
		spendDetails.add(member);
		spendDetails.add(new JLabel("Points spent"));
		spendDetails.add(pointsSpent);
		spendDetails.add(new JLabel(""));
		spendDetails.add(save);
		
		SpringUtilities.makeCompactGrid(spendDetails, 2, 6, 0, 0, 5, 5);
		
		this.setLayout(new BorderLayout());
		this.add(spendDetails, BorderLayout.NORTH);
		this.add(spendsScroll, BorderLayout.CENTER);
	}
	
	public void setSaveAction(Action a) {
		save.setAction(a);
	}
	
	public Timestamp getDate()
	{
		return new Timestamp(date.getDate().getTime());
	}
}
